﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.销售台账.service;
using DevExpress.XtraLayout;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraEditors.Popup;
using DevExpress.Utils.Win;
using DXApplicationTangche.UC.生产计划.生产批生产.service;
using DXApplicationTangche.UC.销售台账.model;
using System.Web.UI.WebControls;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.Data;
using DevExpress.XtraGrid;
using DiaoPaiDaYin;
using mendian;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.订单一览新.订单预览新;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.订单.service;
using DXApplicationTangche.UC.样品管理.样品订单预览;

namespace DXApplicationTangche.UC.销售台账
{
    public partial class Form销售台账 : DevExpress.XtraEditors.XtraForm
    {
        private String shopId { get; set; }
        private String shopName { get; set; }
        private double total金额总计 { get; set; }
        private List<OrderStatusSaleIndexDTO> list = new List<OrderStatusSaleIndexDTO>();
        private List<OrderDailyConfirmDTO> OrderDailyConfirmDTOs = new List<OrderDailyConfirmDTO>();
        public Form销售台账()
        {
            InitializeComponent();
            this.searchLookUpEdit选择店铺.Properties.DataSource = 生产批生产Service.get店铺一览();
            this.bandedGridView销售台账.GroupSummary.AddRange(new GridSummaryItem[] {
                new GridGroupSummaryItem(SummaryItemType.Custom, "totalPrice", null, "(金额总计= {0})"),
            });
            
        }
        /// <summary>
        /// 套号
        /// </summary>
        /// <param name="str"></param>
        public Form销售台账(String str)
        {
            InitializeComponent();
            this.dateTimePicker日期.Value = DateTime.Now;
            this.searchLookUpEdit选择店铺.Properties.DataSource = 生产批生产Service.get店铺一览();
            this.bandedGridView销售台账.GroupSummary.AddRange(new GridSummaryItem[] {
                new GridGroupSummaryItem(SummaryItemType.Custom, "totalPrice", null, "(金额总计= {0})"),
            });
            this.shopId = "50";
            this.shopName = "套号";
            if (string.IsNullOrEmpty(this.shopId))
            {
                MessageBox.Show("请选择店铺");
                return;
            }
            try
            {
                this.list = new 销售台账Model().订单状态尺寸清洗(
                    销售台账Service.get筛选销售台账(
                        this.dateTimePicker日期.Value.ToString("yyyy-MM-dd"),
                        this.shopId
                        )
                    );
            }
            catch
            {
                MessageBox.Show("无查询信息");
            }
            this.gridControl销售台账.DataSource = this.list;
            this.bandedGridView销售台账.ActiveFilter.NonColumnFilter = "[orderStatus] <> '取消'";
            this.bandedGridView销售台账.RefreshData();
        }
        #region 选择店铺
        private void searchLookUpEdit选择店铺_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit选择店铺View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit选择店铺.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.shopId = this.searchLookUpEdit选择店铺.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.shopName = this.searchLookUpEdit选择店铺.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit选择店铺.ToolTip = null;
            searchLookUpEdit选择店铺.EditValue = null;
        }
        private void searchLookUpEdit选择店铺_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.shopName;
        }
        #endregion

        
        private void simpleButton筛选_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.shopId))
            {
                MessageBox.Show("请选择店铺");
                return;
            }
            try
            {
                this.list = new 销售台账Model().订单状态尺寸清洗(
                    销售台账Service.get筛选销售台账(
                        this.dateTimePicker日期.Value.ToString("yyyy-MM-dd"), 
                        this.shopId
                        )
                    );
            }
            catch
            {
                MessageBox.Show("无查询信息");
            }
            this.gridControl销售台账.DataSource = this.list;
            this.bandedGridView销售台账.ActiveFilter.NonColumnFilter = "[orderStatus] <> '取消'";
            this.bandedGridView销售台账.RefreshData();
        }

        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            if (this.list.Count > 0)
            {
                foreach (OrderStatusSaleIndexDTO 销售台账DTO in this.list)
                {
                    this.OrderDailyConfirmDTOs.Add(new OrderDailyConfirmDTO(销售台账DTO));
                }
                销售台账Service.post确认销售台账(this.OrderDailyConfirmDTOs);
                MessageBox.Show("确认完成");
            }
        }

        private void bandedGridView销售台账_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs e)
        {
            if (e.SummaryProcess == CustomSummaryProcess.Start)
	        {
                total金额总计 = 0;
            }
            if (e.SummaryProcess == CustomSummaryProcess.Calculate)
            {
                if (e.FieldValue != null && e.FieldValue != DBNull.Value)
                    if (e.IsGroupSummary) total金额总计 += (double)e.FieldValue;
                if (e.IsGroupSummary)
                    e.TotalValue = total金额总计;
            }
            
        }

        private void 取消订单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String orderId = this.bandedGridView销售台账.GetRowCellValue(this.bandedGridView销售台账.FocusedRowHandle, "orderId").ToString();
                DTO返回值判断 dTO返回值判断 = 订单Service.get清单取消判断(orderId);
                if ("200".Equals(dTO返回值判断.errorCode))
                {
                    RestCall.PostHttp(URL.接口url + "/pmc/orderCancel", orderId);
                    MessageBox.Show("已改为取消订单");
                }
                else
                {
                    MessageBox.Show(dTO返回值判断.errorMessage);
                }
            }
            catch
            {
                MessageBox.Show("修改订单状态失败");
            }
        }

        private void bandedGridView销售台账_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if ("48".Equals(this.shopId))
            {
                new Form样品订单预览新(
                    FunctionHelper.GetbandedgridViewValue(
                        this.bandedGridView销售台账,
                        this.bandedGridView销售台账.FocusedRowHandle,
                        "orderId"
                        )
                    ).ShowDialog();
            }
            else
            {
                new Form订单预览新(
                    FunctionHelper.GetbandedgridViewValue(
                        this.bandedGridView销售台账,
                        this.bandedGridView销售台账.FocusedRowHandle,
                        "orderId"
                        )
                    ).ShowDialog();
            }
        }

        private void simpleButton导出_Click(object sender, EventArgs e)
        {
            if (this.list.Count == 0)
            {
                MessageBox.Show("无导出数据");
                return;
            }
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "导出Excel";
            saveFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
            DialogResult dialogResult = saveFileDialog.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsxExportOptionsEx op = new DevExpress.XtraPrinting.XlsxExportOptionsEx();

                op.ExportType = DevExpress.Export.ExportType.WYSIWYG;
                this.bandedGridView销售台账.OptionsPrint.PrintHeader = true;
                this.bandedGridView销售台账.OptionsPrint.AutoWidth = false;
                this.bandedGridView销售台账.ExportToXlsx(saveFileDialog.FileName, op);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}